/**
 * 
 */
package com.ttl.erp.order.yd;

import java.util.ArrayList;
import java.util.List;

import hibernate.util.HibernateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderBean;
import com.ttl.bc.ap.purchaseorder.chemical.ChemicalDAO;
import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.MethodUtils;

/**
 *  @author :  Surasak Lekmart
 *  Created :  26 Jun 2012 14:28:38 
 *  Project :  ittl
 */
public class TTL_ORDER_YD_BO {

	public boolean showFind(HttpServletRequest request) throws Exception {
		
		String	itemFind = CommonUtils.getItemFind(request);
										
		String hql = "";
		if(itemFind==null||itemFind.trim().equals("")) {
			hql = "FROM TTL_ORDER_YD " +
					 " ORDER BY order_no, col_no, col_sub ";			
		}else {
			hql = "FROM TTL_ORDER_YD " +
					 " WHERE order_no like '%"+itemFind.trim()+"%' "+					 					 
					 " ORDER BY order_no, col_no, col_sub ";
		}
						
		return MethodUtils.showPage(request,hql, HibernateUtil.getSessionFactory2());  
	}

	public boolean add(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		ArrayList<TTL_ORDER_YD> al = getTTL_ORDER_YD_LIST(form, request);
		if (new TTL_ORDER_YD_DAO().addList(al)) {
			
			TTL_ORDER_YD ord = (TTL_ORDER_YD)al.get(0);
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			request.setAttribute("forwardPage", "3;url=orderyd.do?mode=showFind&itemFind=");
			request.setAttribute("itemFind", ord.getOrder_no());
			
			return true;
		}else {
			return false;
		}
		
	}

	private ArrayList<TTL_ORDER_YD> getTTL_ORDER_YD_LIST(ActionForm form,
			HttpServletRequest request) {
		
		ArrayList<TTL_ORDER_YD> al = new ArrayList<TTL_ORDER_YD>();
		
		TTL_ORDER_YD_Form yf = (TTL_ORDER_YD_Form)form;
		System.out.println("yf.getOrder_NO() = "+yf.getOrder_NO());
		
		for (int i = 0; i < yf.getCOL_NO().length; i++) {
			TTL_ORDER_YD ord = new TTL_ORDER_YD();
			ord.setOrder_no(yf.getOrder_NO());
			ord.setOrder_date(CommonUtils.convertStringToDate(yf.getDmy()));
			ord.setOrder_plan(yf.getOrder_Plan());
			ord.setTtl_plan(yf.getPlan());
			ord.setCol_no(yf.getCOL_NO()[i]);
			ord.setCol_sub(yf.getCOL_SUB()[i]);
			ord.setLot_no(yf.getLOT_NO()[i]);
			ord.setCount(yf.getCOUNT()[i]);
			
			System.out.println("yf.getPBLS()[i] = "+yf.getPBLS()[i]);
			if(CommonUtils.isNumber(yf.getPBLS()[i])) {
				ord.setPbls(Float.valueOf(yf.getPBLS()[i]));
			}else {
				ord.setPbls(0f);	
			}
			
			System.out.println("yf.getLBS()[i] ="+yf.getLBS()[i]);
			if(CommonUtils.isNumber(yf.getLBS()[i])) {
				ord.setLbs(Float.valueOf(yf.getLBS()[i]));
			}else {
				ord.setLbs(0f);	
			}
			
			ord.setShade(yf.getSHADE()[i]);
			ord.setW_p(yf.getWP()[i]);
			ord.setRec(yf.getREC()[i]);
			ord.setOrg(yf.getORG()[i]);
			ord.setCheese(yf.getCHEESE()[i]);
			ord.setFuai(yf.getFUAI()[i]);
			ord.setIsCancel(0);
			al.add(ord);
		}
		
		return al;
	}

	public boolean view(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		String rowOrder = request.getParameter("rowOrder");
		String hql = "FROM TTL_ORDER_YD WHERE rowOrder="+rowOrder+"";
		List al = new TTL_ORDER_YD_DAO().find(hql);
		if(al!=null && al.size()>0) {
			
			request.setAttribute("p", al.get(0));
			
			
			return true;
		}else {
			return false;
		}
	}

	public boolean update(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		int rowOrder = Integer.valueOf(request.getParameter("rowOrder"));
		
		ArrayList<TTL_ORDER_YD> al = getTTL_ORDER_YD_LIST(form, request);
		if(al!=null && al.size()>0) {
			TTL_ORDER_YD ord = (TTL_ORDER_YD)al.get(0);
			
			if(new TTL_ORDER_YD_DAO().updateList(ord, rowOrder)) {
				
				request.setAttribute("message", "แก้ไขข้อมูลเรียบร้อย");
				request.setAttribute("forwardPage", "3;url=orderyd.do?mode=showFind&itemFind=");
				request.setAttribute("itemFind", ord.getOrder_no());
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}



	public boolean delete(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int rowOrder = Integer.valueOf(request.getParameter("rowOrder"));
		String order_noOld = request.getParameter("order_noOld");
		
		if( new TTL_ORDER_YD_DAO().delete(rowOrder)) {
			request.setAttribute("message", "ลบ rowOrder:"+rowOrder+" ข้อมูลเรียบร้อย");
			request.setAttribute("forwardPage", "3;url=orderyd.do?mode=showFind&itemFind=");
			request.setAttribute("itemFind", order_noOld);
			return true;
		}else {
			return false;
		}
		
	}

}
